<!--
  * 搜索l历史匹配地址
-->
<template>
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-form-item label="用户地址" class="smart-query-form-itme" name="userCertAddress">
        <a-input style="width: 500px" v-model:value="queryForm.userCertAddress" placeholder="请输入地址" />
      </a-form-item>
      <a-form-item label="楼盘名称" class="smart-query-form-itme" name="communityName">
        <a-input style="width: 500px" v-model:value="queryForm.communityName" placeholder="请输入楼盘名称" />
      </a-form-item>

      <a-form-item class="smart-query-form-item smart-margin-left10">
        <a-button type="primary" @click="ajaxQuery">
          <template #icon>
            <SearchOutlined />
          </template>
          查询
        </a-button>
        <a-button @click="resetQuery">
          <template #icon>
            <ReloadOutlined />
          </template>
          重置
        </a-button>
        <a-button @click="initHis" type="primary"   v-privilege="'as:history:init'" class="smart-margin-left10">
          <template #icon>
            <CloudUploadOutlined />
          </template>
          初始化历史库
        </a-button>
      </a-form-item>
    </a-row>
  </a-form>

  <a-card size="small" :bordered="false" :hoverable="true">
    <a-table
      :scroll="{ x: 1300 }"
      size="small"
      :dataSource="tableData"
      :columns="columns"
      rowKey="addrId"
      :pagination="false"
      :loading="tableLoading"
      bordered
    >
      <template #bodyCell="{ column, record, text }">
        <template v-if="column.dataIndex === 'userCertAddress'">
          <div v-html="text">
          </div>
        </template>
        <template v-if="column.dataIndex === 'communityName'">
          <div v-html="text">
          </div>
        </template>
      </template>
    </a-table>
    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize"
        v-model:current="queryForm.pageIndex"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="ajaxQuery"
        @showSizeChange="ajaxQuery"
        :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-card>
</template>
<script setup lang="ts">
  import { reactive, ref, onMounted,computed } from 'vue';
  import { asApi } from '/@/api/addrstandard/as-api'
  import { smartSentry } from '/@/lib/smart-sentry';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { message } from 'ant-design-vue';
  import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '/@/constants/common-const';

  // --------------------------- 企业表格 列 ---------------------------

  const columns = ref([
    {
      title: '证载地址编码',
      dataIndex: 'houseCode',
      minWidth: 180,
      ellipsis: true,
    },
    {
      title: '证载地址',
      dataIndex: 'certAddress',
      //minWidth: 180,
       ellipsis: true,
    },
    {
      title: '用户地址',
      dataIndex: 'userCertAddress',
     // width: 180,
      ellipsis: true,
    },
    {
      title: '楼盘名称',
      dataIndex: 'communityName',
     // width: 180,
      ellipsis: true,
    },
    {
      title: '是否有效',
      dataIndex: 'status',
     // width: 180,
    },

  ]);

  // --------------------------- 查询 ---------------------------

  const queryFormState = {
    communityName: undefined,
    userCertAddress: undefined,
    pageIndex: 1,
    pageSize: PAGE_SIZE,
  };
  const queryForm = reactive({ ...queryFormState });
  const tableLoading = ref(false);
  const tableData = ref([]);
  const total = ref(0);

  function resetQuery() {
    Object.assign(queryForm, queryFormState);
    ajaxQuery();
  }

  async function ajaxQuery() {
    try {
      if(!queryForm.communityName&&!queryForm.userCertAddress)
        message.error("搜索条件不能为空！")
      else{
        tableLoading.value = true;
        let responseModel = await asApi.searchHistoryPage(queryForm);
        tableData.value = responseModel.data.data;
        total.value = responseModel.data.total;
      }
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      tableLoading.value = false;
    }
  }

  async function initHis() {
      SmartLoading.show();
      try {
        let res = await asApi.initHistoryMatch();
        message.success(res.message);
      } catch (error) {
        smartSentry.captureError(error);
      } finally {
        SmartLoading.hide();
      };
  }
</script>
